package com.zijie.shoprbacapi.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.core.util.JsonResult;
import com.zijie.rbac.model.Perm;
import com.zijie.rbac.model.Resource;
import com.zijie.rbac.service.PermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/perm")
public class PermApi {
    private PermService permService;
@Autowired
    public void setPermService(PermService permService) {
        this.permService = permService;
    }
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(
            @RequestParam(defaultValue = "1")Integer pageNo,
            @RequestParam(defaultValue = "10")Integer pageSize
            , Perm perm) {
        Page<Perm> page = Page.of(pageNo, pageSize);
        page=permService.findAll(page, perm);
//        System.out.println("1111111111111111111111111111111111111111111111");
        return ResponseEntity.ok(JsonResult.success(page));
    }
    @PostMapping("/add")
    public ResponseEntity<JsonResult<?>> add(Perm perm) {
        boolean result = permService.add(perm);
        if (result){
            return ResponseEntity.ok(JsonResult.success(perm));
        }
       else {
           return ResponseEntity.ok(JsonResult.error("添加失败"));
        }
    }
    //    修改
    @PutMapping("/update")
    public ResponseEntity<JsonResult<?>> update(Perm perm) {
        boolean result = permService.update(perm);
        if (result){
            return ResponseEntity.ok(JsonResult.success(perm));
        }else{
            return ResponseEntity.ok(JsonResult.error("修改失败"));
        }

    }
//    删除多个
    @DeleteMapping("/delete")
    public ResponseEntity<JsonResult<?>> delete(@RequestParam("ids") List<Integer> ids) {
        int result = permService.deleteByIds(ids);
        if(result==0){
            return ResponseEntity.ok(JsonResult.error("删除失败"));
        }else{
            return ResponseEntity.ok(JsonResult.success(result));
        }

    }
    @DeleteMapping("/deleteById/{id}")
    public ResponseEntity<JsonResult<?>> deleteById(@PathVariable Integer id) {
        int result = permService.deleteById(id);
        if(result==1){
            return ResponseEntity.ok(JsonResult.success(result));
        }else{
            return ResponseEntity.ok(JsonResult.error("删除失败"));
        }

    }
    @GetMapping("/findById/{id}")
    public ResponseEntity<JsonResult<?>> findById(@PathVariable Integer id) {
        Perm perm = permService.findById(id);
        if (perm==null){
            return ResponseEntity.ok(JsonResult.error("未查询到指定编号的权限"));
        }else{
            return ResponseEntity.ok(JsonResult.success(perm));
        }
    }
    //分配权限
    @PostMapping("/assignResources")
    public ResponseEntity<JsonResult<?>> assignResources(@RequestBody Map<String, Object> payload) {
        Integer permId = (Integer) payload.get("permId");
        List<Integer> resourceIdsList = (List<Integer>) payload.get("resourceIds");
        Integer[] resourceIds = resourceIdsList != null ? resourceIdsList.toArray(new Integer[0]) : new Integer[0];
        int result = permService.assignResources(permId, resourceIds);
        if (result == 0) {
            return ResponseEntity.ok(JsonResult.error("分配权限失败"));
        } else {
            return ResponseEntity.ok(JsonResult.success(result));
        }

    }
    //查看当前权限
    @GetMapping("/resources/{permId}")
    public ResponseEntity<JsonResult<?>> resources(@PathVariable Integer permId) {
        List<Resource> result = permService.resources(permId);
        if (result == null) {
            return ResponseEntity.ok(JsonResult.error("查询权限失败"));
        } else {
            return ResponseEntity.ok(JsonResult.success(result));
        }
    }
    //删除某权限所含的资源
    @DeleteMapping("/removeResource")
    public ResponseEntity<JsonResult<?>> deleteResources(@RequestParam Integer permId, @RequestParam Integer resourceId) {
        int result = permService.deleteResources(permId, resourceId);
        if (result == 0) {
            return ResponseEntity.ok(JsonResult.error("删除权限失败"));
        } else {
            return ResponseEntity.ok(JsonResult.success(result));
        }
    }


}
